package com.sun.lock;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReentrantReadWriteLockDemo {
	private ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();

	public static void main(String[] args) {
		final ReentrantReadWriteLockDemo test = new ReentrantReadWriteLockDemo();

		new Thread("A") {
			public void run() {
				test.get(Thread.currentThread());
			};
		}.start();

		new Thread("B") {
			public void run() {
				test.get(Thread.currentThread());
			};
		}.start();
	}

	public void get(Thread thread) {
		rwl.readLock().lock(); // 在外面获取锁
		try {
			long start = System.currentTimeMillis();
			System.out.println("线程" + thread.getName() + "开始读操作...");
			while (System.currentTimeMillis() - start <= 1) {
				System.out.println("线程" + thread.getName() + "正在进行读操作...");
			}
			System.out.println("线程" + thread.getName() + "读操作完毕...");
		} finally {
			rwl.readLock().unlock();
		}
	}
}
